package com.northpool.operator.cicd.notice;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.utils.StpOperatorHelper;

@OperatorInfo(
        ref = "WeChatBootOperator",
        name = "企业微信通知",
        description = "企业微信发送文本通知",
        version = "text"
)
public class WeChatBotTextOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "webhook", name = "WebHook地址", desc = "WebHook地址, 如: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1abb0828-9c74-4e30-a1b0-cfa17c82512a", required = true),
            @InputParam(ref = "content", name = "文本内容", desc = "文本内容, 如: hello", required = true),
            @InputParam(ref = "mentionedList", name = "用户列表", desc = "userid的列表，提醒群中的指定成员(@某个成员)，@all表示提醒所有人，如果开发者获取不到userid，可以使用mentioned_mobile_list", value = "@all"),
            @InputParam(ref = "mentionedMobileList", name = "手机号列表", desc = "手机号列表，提醒手机号对应的群成员(@某个成员)，@all表示提醒所有人", value = "")
    })
    public WeChatBotTextOperator() {
    }

    public static void main(String[] args) {
        OperatorTestBuild.execute(WeChatBotTextOperator.class, TestParamBuilder.aBuilder()
                .param("webhook", "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1abb0828-9c74-4e30-a1b0-cfa17c82512a")
                .param("content", "test")
                .param("mentionedList", "")
                .param("mentionedMobileList", "17796756787")
                .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String webhook = paramTool.getStr("webhook");
        String content = paramTool.getStr("content");
        String mentionedList = paramTool.getStr("mentionedList");
        String mentionedMobileList = paramTool.getStr("mentionedMobileList");

        String format = StrUtil.format("{\"msgtype\":\"text\",\"text\":{\"content\":\"{}\",\"mentioned_list\":\"{}\",\"mentioned_mobile_list\":\"{}\"}}"
                , content, mentionedList, mentionedMobileList);
        HttpResponse response = HttpUtil.createPost(webhook)
                .body(format)
                .timeout(10000)
                .execute();
        if (response.isOk()) {
            JSONObject resObj = JSONObject.parseObject(response.body());
            String errmsg = resObj.getString("errmsg");
            if ("ok".equals(errmsg)) {
                this.log("发送成功");
            } else {
                this.log("发送失败, 响应体: {}", response.body());
            }
        } else {
            this.log("发送失败, 响应状态码: {}, 响应体: {}", response.getStatus(), response.body());
        }
    }
}
